﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class Admin_WebContent : AdminPage
{
    TblItemDetailDAO itemDetailDAO = new TblItemDetailDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pnlList.Visible = true;
            pnlEdit.Visible = false;
            loaddata();
        }
        else
        {
            if (Request.QueryString["id"] != null && Request.QueryString["idSP"] != "" && Request.QueryString["idDetails"] !=null)
            {
                pnlList.Visible = false;
                pnlEdit.Visible = true;
                if (Request.Params["__EVENTTARGET"] != null)
                {
                    if (Request.Params["__EVENTTARGET"].IndexOf("cmdEdit") > 0)
                    {
                        LoadDetail();
                    }
                    if (Request.Params["__EVENTTARGET"].IndexOf("cmdEdit1") > 0)
                    {
                        LoadDetail();
                    }
                    if (Request.Params["__EVENTTARGET"].IndexOf("cmdEdit2") > 0)
                    {
                        LoadDetail();
                    }
                    if (Request.Params["__EVENTTARGET"].IndexOf("cmdEdit3") > 0)
                    {
                        LoadDetail();
                    }
                    if (Request.Params["__EVENTTARGET"].IndexOf("cmdEdit4") > 0)
                    {
                        LoadDetail();
                    }
                    if (Request.Params["__EVENTTARGET"].IndexOf("cmdEdit5") > 0)
                    {
                        LoadDetail();
                    }
                    if (Request.Params["__EVENTTARGET"].IndexOf("cmdEdit6") > 0)
                    {
                        LoadDetail();
                    }
                    if (Request.Params["__EVENTTARGET"].IndexOf("cmdEdit7") > 0)
                    {
                        LoadDetail();
                    }
                    if (Request.Params["__EVENTTARGET"].IndexOf("cmdEdit8") > 0)
                    {
                        LoadDetail();
                    }
                }

            }
        }
    }

    private void LoadDetail()
    {
        string idItem = Request.QueryString["idItem"];
        string id = Request.QueryString["id"];
        string idSP = Request.QueryString["idSP"];
        string idDetails = Request.QueryString["idDetails"];

        try
        {

            TblItemDetail itemDetail = new TblItemDetail();
            itemDetail = itemDetailDAO.GetEdit(Global.ConvertToInt(idDetails), Global.ConvertToInt(idSP));
            txtTitle.Text = id;

            if (id == "TinhNangNoiBat")
            {
                txtcontent.Content = Server.HtmlDecode(itemDetail.TinhNangNoiBat);
            }
            if (id == "GiaiTri")
            {
                txtcontent.Content = Server.HtmlDecode(itemDetail.GiaiTri);
            }
            if (id == "UngDungvaTroChoi")
            {
                txtcontent.Content = Server.HtmlDecode(itemDetail.UngDungvaTroChoi);
            }
            if (id == "ThongTinChung")
            {
                txtcontent.Content = Server.HtmlDecode(itemDetail.ThongTinChung);
            }
            if (id == "NhacChuong")
            {
                txtcontent.Content = Server.HtmlDecode(itemDetail.NhacChuong);
            }
            if (id == "Nguon")
            {
                txtcontent.Content = Server.HtmlDecode(itemDetail.Nguon);
            }
            if (id == "KetNoiDuLieu")
            {
                txtcontent.Content = Server.HtmlDecode(itemDetail.KetNoiDuLieu);
            }
           
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
    }
    private void loaddata()
    {
        string idItem = Request.QueryString["idItem"];
        try
        {
            
            lvData.DataSource = itemDetailDAO.GetItemDetail(Convert.ToInt32(idItem));
            lvData.DataBind();
        }
        catch ( Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }

    }
    protected void cmdSave_Click(object sender, EventArgs e)
    {
        string idItem = Request.QueryString["idItem"];
        bool bFlag = false;
        string id = Request.QueryString["id"];
        string idSP = Request.QueryString["idSP"];
        string idDetails = Request.QueryString["idDetails"];

        try
        {
            
            /*TblItemDetail itemDetail = new TblItemDetail();

            itemDetail.ItemID = Global .ConvertToInt(Request.QueryString["id"]);
            //entity.WebTitle = txtTitle.Text;
            //entity.WebContent = Server.HtmlEncode(txtContent.Content);
            itemDetail.TinhNangNoiBat = Server.HtmlEncode(txtcontent.Content);

            //entity.Page = txtPage.Text;

            //webContentDAO.Update(entity);
            //bFlag = true;

            //  Session["id"] = entity.WebID;

            itemDetailDAO.Insert_Update(itemDetail, 2); */
            itemDetailDAO.UpdateItemDetails(Convert.ToInt32(idDetails), Convert.ToInt32( idSP), id, Server.HtmlEncode(txtcontent.Content));
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN, true);
        }
        if (bFlag)
            Response.Redirect("~/Admin/AddItem.aspx?id=" + idSP, true);
        
            
    }
}
